﻿using ALMDDD.Domain.Core.Bus;
using ALMDDD.Domain.Core.Commands;
using ALMDDD.Domain.Core.Events;
using MediatR;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ALMDDD.Infrastructure.Bus
{
    /// <summary>
    /// 中介者模式记忆总线
    /// </summary>
    public sealed class InMemoryBus : IMeDiatorHandler
    {
        private readonly IMediator _mediator;
        /// <summary>
        /// 注入mediator
        /// </summary>
        /// <param name="mediator"></param>
        public InMemoryBus(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 发布事件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="evnet"></param>
        /// <returns></returns>
        public Task RaiseEvent<T>(T evnet) where T : Event
        {
            return _mediator.Publish(evnet);
        }
        /// <summary>
        /// 发送命令
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="command"></param>
        /// <returns></returns>
        public Task SendCommand<T>(T command) where T : Command
        {
            return _mediator.Send(command);
        }
    }
}
